/**
 * 地址管理路由模块
 * 
 * 实现地址增删改查(CRUD)操作:
 * 1. GET /api/user-address - 获取所有地址列表
 * 2. GET /api/user-address/:id - 根据ID获取特定地址
 * 3. POST /api/user-address - 创建新地址
 * 4. PATCH /api/user-address/:id - 更新地址信息
 * 5. DELETE /api/user-address/:id - 删除地址
 */

//引入express模块
const express = require("express");

//创建路由对象
const router = express.Router();

//引入地址控制器
const addressController = require("../controllers/userAddress.controller");

// 引入公共的异步错误处理工具函数
const asyncHandler = require("../utils/asyncHandler");

/*
  @route GET /api/user-address/
  @desc 根据用户ID获取地址列表
  @access Public
  @query {number} userId - 必填的用户ID参数
  测试用例：
  //用例1：根据用户ID获取地址列表
  curl -X GET http://localhost:3303/api/user-address?userId=1
  //用例2：缺少userId参数
  curl -X GET http://localhost:3303/api/user-address
*/
router.get("/", asyncHandler(addressController.getAllAddresses));


/**
 * @route   GET /api/user-address/:id
 * @desc    根据ID获取特定地址
 * @access  Public
 * 测试用例：
 * //用例1：获取存在的地址
 * curl -X GET http://localhost:3303/api/user-address/1
 * //用例2：获取不存在的地址
 * curl -X GET http://localhost:3303/api/user-address/9999
 */
router.get("/:id", asyncHandler(addressController.getAddressById));



/**
 * @route   POST /api/user-address
 * @desc    创建地址
 * @access  Public
 * 测试用例：
  //用例1：创建新地址
  curl -X POST http://localhost:3303/api/user-address -H "Content-Type: application/json" -d "{\"userId\": 1, \"receiverName\": \"张三\", \"receiverPhone\": \"13800138000\", \"provinceName\": \"广东省\", \"cityName\": \"深圳市\", \"districtName\": \"南山区\", \"detail\": \"科技园南区001\", \"defaultAddress\": 1}"
  //用例2：创建地址，缺少必填字段 receiverName
  curl -X POST http://localhost:3303/api/user-address -H "Content-Type: application/json" -d "{\"userId\": 1, \"receiverPhone\": \"13800138000\", \"provinceName\": \"广东省\", \"cityName\": \"深圳市\", \"districtName\": \"南山区\", \"detail\": \"科技园南区001\", \"defaultAddress\": 1}"
*/
router.post("/", asyncHandler(addressController.createAddress));


/**
 * @route   PATCH /api/user-address/:id
 * @desc    更新地址信息
 * @access  Public
 * 测试用例：
  //用例1：更新存在的地址
  curl -X PATCH http://localhost:3303/api/user-address/1 -H "Content-Type: application/json" -d "{\"receiverName\": \"李四\", \"defaultAddress\": 1}"
  //用例2：更新不存在的地址
  curl -X PATCH http://localhost:3303/api/user-address/9999 -H "Content-Type: application/json" -d "{\"receiverName\": \"李四\", \"defaultAddress\": 1}"
  //用例3：更新地址，参数错误
  curl -X PATCH http://localhost:3303/api/user-address/1 -H "Content-Type: application/json" -d "{\"receiverName\": \"\"}"
*/
router.patch("/:id", asyncHandler(addressController.updateAddress))

/**
 * @route   DELETE /api/user-address/:id
 * @desc    删除地址
 * @access  Public
 * 测试用例：
  //用例1：删除存在的地址
  curl -X DELETE http://localhost:3303/api/user-address/1
  //用例2：删除不存在的地址
  curl -X DELETE http://localhost:3303/api/user-address/9999
 */
router.delete("/:id", asyncHandler(addressController.deleteAddress));

//导出路由对象，供app.js使用
module.exports = router;